package com.ulook.lookplanet.service;

import android.app.IntentService;
import android.content.Intent;

import androidx.annotation.Nullable;

import com.ulook.lookplanet.common.AppServer;
import com.ulook.lookplanet.data.model.TSysMsg;
import com.ulook.lookplanet.data.model.TUserMsg;
import com.ulook.lookplanet.util.json.JacksonUtil;
import com.ulook.lookplanet.util.net.AsynRequestUtil;
import com.ulook.lookplanet.util.net.OkHttpException;
import com.ulook.lookplanet.util.net.RequestParams;
import com.ulook.lookplanet.util.net.ResponseCallback;

import java.util.List;

/**
 * 描述：
 *
 * @author L1998
 * @data 2021/5/2
 */
public class MsgService extends IntentService {

    private static volatile boolean RUNNING = true;
    private boolean isSys;
    public final static String USER_MSG = "user_msg";
    public final static String SYS_MSG = "sys_msg";
    private String sysMsgUrl = AppServer.ROOT_PATH + "/notif/querySysMsg";
    private String userMsgUrl = AppServer.ROOT_PATH + "/notif/queryUserMsg";
    private int currUserId;

    public MsgService(String name, int currUserId){
        super(name);
        this.currUserId = currUserId;
        if(name.equals(SYS_MSG)){
            isSys = true;
        }else {
            isSys = false;
        }
    }

    /**
     * 结束服务
     */
    public void stopService(){
        RUNNING = false;
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        RequestParams params = new RequestParams();
        params.put("userid", currUserId);
        ResponseCallback callback = new ResponseCallback() {
            @Override
            public void onSuccess(Object res) {
                saveMsg(res);
            }
            @Override
            public void onFailure(OkHttpException failuer) {

            }
        };
        int delay;
        if(isSys){
            delay = 5000;
        }else {
            delay = 3000;
        }
        while (RUNNING){
            // 查询系统消息
            if(isSys){
                AsynRequestUtil.post(sysMsgUrl, params, null, callback);
            // 查询用户消息
            }else {
                AsynRequestUtil.post(userMsgUrl, params, null, callback);
            }
            try {
                Thread.sleep(delay);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 保存到SQLite
     */
    private void saveMsg(Object msgs){
        try {
            if(isSys){
                List<TSysMsg> sysMsgs = JacksonUtil.json2list((String)msgs, TSysMsg.class);
                for (int i = 0; i < sysMsgs.size(); i++){
                    sysMsgs.get(i).save();
                }
            }else {
                List<TUserMsg> userMsgs = JacksonUtil.json2list((String)msgs, TUserMsg.class);
                for (int i = 0; i < userMsgs.size(); i++){
                    userMsgs.get(i).save();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
